package cn.iocoder.lw.module.core.controller.admin.project;

import cn.iocoder.lw.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.lw.framework.common.pojo.CommonResult;
import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.common.util.object.BeanUtils;
import cn.iocoder.lw.framework.excel.core.util.ExcelUtils;
import cn.iocoder.lw.module.core.controller.admin.project.vo.*;
import cn.iocoder.lw.module.core.service.project.CoreProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.lw.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.lw.framework.common.pojo.CommonResult.success;
import static cn.iocoder.lw.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
import static cn.iocoder.lw.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 核心项目")
@RestController
@RequestMapping("/core/project")
@Validated
public class CoreProjectController {

    @Resource
    private CoreProjectService projectService;

    @PostMapping("/create")
    @Operation(summary = "创建项目")
    @PreAuthorize("@ss.hasPermission('core:project:create')")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Long> createProject(@Valid @RequestBody CoreProjectSaveReqVO createReqVO) {
        return success(projectService.createProject(createReqVO, getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目")
    @PreAuthorize("@ss.hasPermission('core:project:update')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updateProject(@Valid @RequestBody CoreProjectSaveReqVO updateReqVO) {
        projectService.updateProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:project:delete')")
    @ApiAccessLog(operateType = DELETE)
    public CommonResult<Boolean> deleteProject(@RequestParam("id") Long id) {
        projectService.deleteProject(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('core:project:query')")
    public CommonResult<CoreProjectRespVO> getProject(@RequestParam("id") Long id) {
        CoreProjectRespVO project = projectService.getProject(id);
        return success(project);
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目分页")
    @PreAuthorize("@ss.hasPermission('core:project:query')")
    public CommonResult<PageResult<CoreProjectRespVO>> getProjectPage(@Valid CoreProjectPageReqVO pageReqVO) {
        PageResult<CoreProjectRespVO> pageResult = projectService.getProjectPage(pageReqVO);
        // 拼接数据
        pageResult.getList().forEach(project -> {
        });
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目 Excel")
    @PreAuthorize("@ss.hasPermission('core:project:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectExcel(@Valid CoreProjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PAGE_SIZE_NONE);
        PageResult<CoreProjectImportExcelVO> pageResult = projectService.getProjectList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "项目数据.xls", "项目列表", CoreProjectImportExcelVO.class,
                BeanUtils.toBean(pageResult.getList(), CoreProjectImportExcelVO.class));
    }

    @GetMapping("/owner-user")
    @Operation(summary = "获得负责人名下的项目列表")
    @Parameter(name = "ownerUserId", description = "负责人用户编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('core:project:query')")
    public CommonResult<List<CoreProjectRespVO>> getProjectListByOwnerUserId(@RequestParam("ownerUserId") Long ownerUserId) {
        List<CoreProjectRespVO> list = projectService.getProjectListByOwnerUserId(ownerUserId);
        return success(list);
    }

}